﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;

namespace Fitment.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class TaskService
    {


        [OperationContract]

        public List<Task> GetTasks(int id)
        {
            MyLinqDataContext data = new MyLinqDataContext();  //新建数据上下文

            var main = (from q in data.Material
                        from o in data.WorkerInfo
                        from i in data.SelectItem
                        from m in data.RoomInfo
                        where o.WorkType == q.WorkType && i.Room_ID==m.Room_ID &&m.House_ID==id &&i.Mat_ID==q.Mat_ID
                        orderby o.Priority ascending 
                        select new { o.WorkType,o.Priority }).Distinct();

            List<Task> nodes = new List<Task>();  //新建任务结点
            int count = 0;  //用于计数子任务的多少
            foreach (var find in main)
            {
                //用于获取父项目开始和结束的时间
                var date = (from p in data.ProjectInfo
                            from q in data.Material
                            where p.ProjectName == q.ProName && q.WorkType == find.WorkType && p.House_ID == id &&Convert.ToInt32(p.StartDate)!=null
                            orderby p.StartDate descending
                            select new { p.StartDate, p.FinishDate }).Take(1);
                if(date.Count()==0)
                {
                    return nodes;
                }
                foreach (var read in date)
                {
                    //添加父节点值
                    nodes.Add(new Task { TaskName = find.WorkType + "进场", StartDate = read.StartDate, EndDate = read.FinishDate, Resources = "材料清单", PercentComplete = 25d });

                }


                nodes[count].Children = new List<Task>();//增加子结点信息

                //对于特定工程增加子结点
                var child = (from p in data.ProjectInfo
                             from o in data.WorkerInfo
                             from q in data.Material
                             from i in data.SelectItem
                             where p.ProjectName == q.ProName && q.WorkType == find.WorkType && p.House_ID == id && i.Mat_ID==q.Mat_ID
                             orderby p.StartDate descending
                             select new { p.ProjectName, p.StartDate, p.FinishDate, q.MatName }).Distinct();


                foreach (var search in child)
                {
                    nodes[count].Children.Add(new Task { TaskName = search.ProjectName, StartDate = search.StartDate, EndDate = search.FinishDate, Resources = search.MatName, PercentComplete = 25d });

                }
                count++;
            }

            return nodes;



        }
        // Add more operations here and mark them with [OperationContract]
    }
}
